import TableData from "@/components/public/el-table-pagination"; //主页面列表

import TableLyout from '@/components/commonPublic/tableLyout.vue';
import tree from "@/components/PhysicalQualityPage/rebarThicknessManage/el-tree"; //左侧机构树


import CustomDialog from "@/components/PhysicalQualityPage/reboundStrengthManage/reboundDetectionEquipment/index.vue";
import {
    getMangEquipmentPageList, //列表页接口
    delMangEquipment, //删除
    submitMangEquipment, //提交
    getUserCompanyTreeList, //树状图
} from "@/api/request.js";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置
export default {
    name: "rebarDetectionEquipment",
    data() {
        return {
            name: "rebarDetectionEquipment",
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            // 机构树
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            // checked: true, //查看下级按钮
            showName: "未选择公司",
            checkData: [], //树状图默认选中id


            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            radio: "待办",
            // 列表
            getList: {
                modelType: "2", // 0代办 1已办
                fCompanyid: null, //机构id
                fUserid: null, //当前登录用户id
                sublevel: null, //查看下级  1是0否
                keyWord: "", //设备名称模糊查询字段
                keyType: null, //设备类型
                page: 1, //页码
                pageSize: 20, //每页条数
            },
            isShow: false, //是否显示弹框

            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,

        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        tree,
        elBreadcrumb,
        CustomDialog
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.yj_searchForm(this.getList);
        this.getTree();
        this.btnRole(this.name)
    },
    methods: {
        refreshList() { //刷新列表

            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },

        closeDialog() {
            this.isShow = false;
        },

        // 左侧树结构
        getTree() {
            this.treeText.companyId = this.userInfo.companyId;
            this.treeText.userId = this.userInfo.userId;
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("yjSmart-rebarThicknessManage_treeCheck")
                            ).fShortname;
                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }

                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fShortname; //顶部显示机构名
            this.getList.fCompanyid = data.fCompanyid;
            this.getList.page = 1;
            this.getList.pageSize = 20;
            this.yj_searchForm(this.getList);
        },
        handleRow() { },
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },
        // 新增/编辑
        yj_edit(num) {
         
             if (num == 7) { //查看
                    if (this.dataFid != null) {
                        this.statusNum = 7
                        this.isShow = true;
                        this.editText();
                    } else {
                        this.$message.warning("请先选择需要查看的数据");

                    }
                }else if(num==1){
                    this.$message.warning("该功能正在卖力开发中"); 
                    // if (this.dataFid != null) {
                    //     this.$message.warning("该功能正在卖力开发中"); 
                    // } else {
                    //     this.$message.warning("请先选择需要打印的数据");
                    // }
                }
           
        },
        editText() {
            this.isShow = true;
        },
        // 删除
        yj_delete() {
            if (this.dataFid != "" && this.dataFid != null) {
                this.$confirm("您是否要永久删除此数据?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.loading = true;
                        this.isLock = true
                        delMangEquipment({
                            fId: this.dataFid,
                        })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.isLock = false
                                    this.loading = false;
                                    this.yj_searchForm(this.getList);
                                    this.$message.success("删除成功");
                                    this.dataFid = null

                                } else {
                                    this.isLock = false
                                    this.$message.error("删除失败", res.msg);
                                    this.loading = false;
                                }
                            })
                            .catch(() => {
                                console.log("删除失败");
                                this.loading = false;
                            });
                    })
                    .catch(() => {
                        this.$message("取消删除");
                    });
            } else {
                return this.$message.error("请先选择需要删除的数据");
            }
        },
        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            if (label == "设备编码") {
                this.currentRow = row;
                this.statusNum = 7
                this.isShow = true;

            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                modelType: "2", // 0代办 1已办
                fCompanyid: data.fCompanyid, //机构id
                fUserid: this.userInfo.userId, //当前登录用户id
                sublevel: 1, //查看下级  1是0否
                keyWord: data.keyWord, //设备名称模糊查询字段
                keyType: "", //设备类型
                page: data.page, //页码
                pageSize: data.pageSize, //每页条数
                fNote: "2",//1回弹  2钢保
            }
            this.loading = true;
            var that = this;
            getMangEquipmentPageList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.mangEquipmentList.list;
                        that.queryCriteriaData = res.data.mangEquipmentList;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })

        },
        // 提交
        yj_submit() {
            if (this.currentRow != null) {
                if (this.dataFid != "" && this.dataFid != null) {
                    this.$confirm("是否继续提交?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.isLock = true
                            this.loading = true;
                            submitMangEquipment({
                                fId: this.dataFid,
                                userId: this.userInfo.userId, //当前登录人id
                                userName: this.userInfo.realName //当前登录人姓名
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.dataFid = null
                                        this.isLock = false
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("提交成功");
                                    } else if (res.errcode == -2) {
                                        this.isLock = false
                                        this.$message.error(res.msg);
                                        this.yj_searchForm(this.getList);
                                        this.loading = false;
                                        return false;
                                    } else {
                                        this.isLock = false
                                        this.$message.error("提交失败", res.msg);
                                        this.loading = false;
                                        return false;
                                    }
                                })
                                .catch(() => {
                                    this.loading = false;
                                    console.log("提交失败");
                                });
                        })
                        .catch(() => {
                            this.$message("取消提交");
                        });
                } else {
                    return this.$message.error("请先选择需要提交的数据");
                }
            }
        },

    },
};